<?php
/**
 * @category   Order
 * @package  Order
 * @subpackage  Order
 * @author    fern <admin@iic.cn>
 * 订单处理综合类
 * 效验产品信息
 * 效验用户配送地址
 * 效验订单财务明细
 * 创建订单
 * 写入订单商品（及扩展）
 * 写入订单折扣
 * 追加订单财务信息
 * 变更订单状态(订单操作类Eorder)
 * 执行流程 初始化类-》校验地址-》校验产品-》写入订单主表
 * -》写入订单扩展-》写入产品-》写入产品扩展-》写入订单地址
 * -》写入财务信息-》写入优惠券信息-》校验订单信息
 */
class Order {
	// 订单基本信息
	protected $orderinfo = array ();
	protected $orderextinfo = array ();
	protected $info = array ();
	
	// 订单商品信息
	protected $ordergoods = array ();
	protected $ordergoodsext = array ();
	protected $goods = array ();
	
	// 订单的配送地址
	protected $orderaddress = array ();
	
	// 订单的财务信息
	protected $orderfinance = array ();
	
	// 订单的优惠折扣信息
	protected $ordercoupon = array ();
	protected $orderdao, $orderextdao, $goodsdao, $addresdao, $financedao, $coupondao, $goodsextdao;
	protected $userid = 0;
	protected $orderid = 0;
	protected $isdebug = 0;
	protected $debug = array ();
	protected $line = __LINE__;
	public $err91yungou = 0;
	protected $platform = '';
	protected $role = '0';
	/**
	 * 初始化订单的信息
	 *
	 * @param array $info
	 *        	订单的基本信息
	 *        	$info=array(
	 *        	'0'=>array('element'=>'','value'=>'','name'=>''),
	 *        	);
	 * @param array $address
	 *        	订单的配送地址
	 * @param number $userid
	 *        	订单的用户ID信息
	 * @param string $ip
	 *        	订单产生的IP信息
	 */
	function __construct($info = array(), $userid = 0, $ip = '', $platform = 'SMW01') {
		self::_ini_info ( $ip, $platform );
		$this->platform = $platform;
		$this->orderinfo ['amount'] = $info ['amount'];
		$this->orderinfo ['count_num'] = $info ['count_num'];
		unset ( $info ['amount'] );
		unset ( $info ['count_num'] );
		if ($platform == 'SMW01') {
			if ($userid == 0) {
				$this->debug = 'userid:' . $userid;
				$err = array (
						"status" => 0,
						'msg' => '用户ID。错误编码：',
						'data' => $this->debug 
				);
				return self::_err ( $err );
			}
			$this->userid = $userid;
			$this->orderinfo ['userid'] = $info ['userid'];
			if ($info ['role']) {
				if(is_array($info['role'])){
					$this->role = $info ['role'];
				}else{
					$this->role = explode(',',$info ['role']);
				}
			}
		} else {
			$this->orderextinfo = $info['ext'];
		}
		$this->orderinfo ['pay_time'] = mktime();
		$this->orderinfo ['coupon'] =$info['psval'];
		
		$this->orderdao = M ( "Order" );
		$this->orderextdao = M ( "OrderExt" );
		$this->goodsdao = M ( "OrderGoods" );
		$this->addresdao = M ( "OrderAddress" );
		$this->financedao = M ( "OrderFinance" );
		$this->coupondao = M ( "OrderCoupon" );
		$this->goodsextdao = M ( "OrderGoodsExt" );
	}
	
	/**
	 * 91云够的错误输出
	 * @date 2013-6-8
	 * @time 下午10:26:43
	 */
	function err91yungou($str = '') {
		// 91云够的错误输出
		exit ( $str );
	} // end err91yungou
	
	/**
	 * 输出错误信息
	 * @date 2013-5-6
	 * @time 下午5:19:24
	 */
	function _err($arr = array(), $str = "操作出现错误。") {
		// 请求接口类型异常
		if (is_array ( $arr ) && ! empty ( $arr )) {
			$res = array (
					"status" => 0,
					"msg" => $str,
					'data' => $arr,
					'line' => $this->line
			);
		} else {
			$res = array (
					"status" => 0,
					"msg" => $str,
					'data' => $this->debug,
					'line' => $this->line 
			);
		}
		$res = json_encode ( $res );
		if ($this->err91yungou == 1) {
			self::err91yungou ( $res );
		}
		return $res;
	} // end _err
	
	/**
	 * 请求成功返回
	 * @date 2011-11-15
	 * @time 上午08:10:19
	 */
	private function _suc($arr = array(), $str = "操作成功") {
		// 请求接口类型异常
		$res = array (
				"status" => 1,
				"msg" => $str,
				'data' => $this->debug 
		);
		if (! empty ( $arr )) {
			$res = array_merge ( $res, $arr );
		}
		echo myjson ( $res );
		die ();
	} // end _suc
	
	/**
	 * 检查产品信息是否完整
	 * 获取用户的产品价格(用户的价格由各平台校验)
	 *
	 * @param $goods=array( array(
	 *        	'psn'=>'123123',//产品的编码
	 *        	'price'=>'10000',//产品售价
	 *        	'number'=>'1',//产品数量
	 *        	'ext'=>array('url'=>'','images'=>'','price'=>'')//其它扩展信息
	 *        	)
	 *        	);
	 */
	function chk_goods($goods = array()) {
		$this->debug = $goods;
		$this->line=__LINE__;
		if (! is_array ( $goods )) {
			$err = array (
					"status" => 0,
					'msg' => '产品信息类型错误。错误编码：',
					'data' => $this->debug 
			);
			return self::_err ( $err );
		}                                        
		$product = D ( "ProductRead" );          
		$err = false;                            
		$data = array ();                        
		$data_ext = array ();                    
		$amount=0;
		$join_arr = array();
		$re = M('site_ext')->field('site_id')->where( array('value'=>1) )->select();
		foreach ( $re as $value ) {
			$join_arr[] = $value['site_id'];
		}
		foreach ( $goods as $v ) {               
			$where = array (
// 					'code' => $v ['product_psn'],
					'id' => $v ['product_id'] 
			);
			$temp = FALSE;
			if ( ($this->platform == '91YUNGOU') && in_array($v['site_id'], $join_arr) ) 
				$temp = self::ext_specia_product( $v['product_id'] );
			$temp =$temp ? $temp : $product->where ( $where )->find ();
			$this->debug = $product->getLastSql ().'  '.__LINE__;
			if (empty ( $temp )) {
				$err = true;
				continue;
			}
			$data_sub = array ();
			$data_sub ['product_id'] = $temp ['id'];
			$data_sub ['product_thumb'] = $temp ['image'];
			$data_sub ['product_name'] = $temp ['name'];
			$data_sub ['product_psn'] = $temp ['code'];
			$data_sub ['mname'] = $temp ['mname'];
			$data_sub ['number'] = $v ['number'];
			if ($this->platform == '91YUNGOU') {
				$data_sub ['price'] = $temp ['jiayi'];
				$data_sub ['priceall'] = $temp ['jiayi'] * $v ['number'];
				$data_sub ['ext'] = array ();
				$data_sub ['ext'] ['saleprice'] = $v ['price'];
				$data_sub ['ext'] ['allsaleprice'] = $v ['price'] * $v ['number'];
				$data_sub ['ext'] ['site_id'] = $v['site_id'];
				$data_sub['ext']['salenumber']=$v['number'];
				$amount+=$data_sub ['priceall'];
				$ext=array();
				$ext['product_thumb']=$v['product_thumb'];
				$ext['product_name']=$v['product_name'];
				$data_sub ['ext'] ['ext'] =serialize($ext);
			} elseif ($this->platform == 'SMW01') {
				$jia = 'jiasi';
				$jiayi=array_intersect($this->role,array(9,10,11,12,13,14,15));
				$jiaer=array_intersect($this->role,array(8));
				$jiasan=array_intersect($this->role,array(7));
				switch (true) {
					case !empty($jiayi):
						$jia = 'jiayi';
						break;
					case !empty($jiaer):
						$jia = 'jiaer';
						break;
					case !empty($jiasan):
						$jia = 'jiasan';
						break;
					default:
						$jia = 'jiasi';
						break;
				}
				$data_sub ['price'] = $temp [$jia];
				$data_sub ['priceall'] = $temp [$jia] * $v ['number'];
				$amount+=$data_sub ['priceall'];
			}
			
			$data [$data_sub ['product_id']] = $data_sub;
			unset ( $v ['price'] );
			unset ( $v ['number'] );
			unset ( $v ['product_psn'] );
			unset ( $v ['product_psn'] );
			unset ( $v ['product_psn'] );
		}
		$this->orderinfo ['amount']=$amount;
		if ($err) {
			$err_ = array (
					"status" => 0,
					'msg' => '校验产品信息出现错误.',
					'data' => $this->debug
			);
			return self::_err ( $err_ );
		}
		$this->goods = $data;
		return $this->goods;
	}
	
	/**
	 * 检查用户配送地址的完整性
	 */
	function chk_address($orderaddress = array()) {
		$address = array ();
		// 是否是登录用户
		$address ['province'] = $orderaddress ['province'];
		$address ['city'] = $orderaddress ['city'];
		$address ['district'] = empty ( $orderaddress ['district'] ) ? '' : $orderaddress ['district'];
		;
		$address ['address'] = $orderaddress ['address'];
		$address ['consignee'] = $orderaddress ['consignee'];
		$address ['mobile'] = $orderaddress ['mobile'];
		$address ['zipcode'] = $orderaddress ['zipcode'];
		$address ['tel'] = $orderaddress ['tel'];
		$address ['mobile'] = empty ( $address ['mobile'] ) ? $address ['tel'] : $address ['mobile'];
		$this->debug = $address;
		if (! $address ['province'] || ! $address ['city'] || ! $address ['address'] || ! $address ['consignee'] || ! $address ['mobile']) {
			$err = array (
					"status" => 0,
					'msg' => '收件人信息不完整，请重试。错误编码：',
					'data' => $this->debug 
			);
			exit ( json_encode ( $err ) );
		}
		$this->orderaddress = $address;
		return $this->orderaddress;
	}
	
	/**
	 * 写入订单 的地址信息
	 */
	function w_address() {
		$this->debug = "order_id:" . $this->orderid;
		if (empty ( $this->orderid )) {
			$err = array (
					"status" => 0,
					'msg' => '写入订单地址信息错误，订单的主ID不存在。错误编码：',
					'data' => $this->debug 
			);
			return self::_err ( $err );
		}
		$this->orderaddress ['order_id'] = $this->orderid;
		$address_id = $this->addresdao->add ( $this->orderaddress );
		$this->debug = $this->addresdao->getlastsql ();
		if ($address_id === false) {
			$err = array (
					"status" => 0,
					'msg' => '写入订单地址信息失败。错误编码：',
					'data' => $this->debug 
			);
			return self::_err ( $err );
		} else {
			return $address_id;
		}
	}
	
	/**
	 * 初始化订单的基本信息
	 * @date 2013-5-1
	 * @time 下午5:01:11
	 */
	function _ini_info($ip = '', $platform = 'SMW01') {
		// 初始化订单的基本信息
		$this->debug = "order_id:" . $this->orderid.'  LINE:'.__LINE__;
		if ($this->orderid != 0) {
			$err = array (
					"status" => 0,
					'msg' => '初始化订单信息之前，订单的主ID不应存在。错误编码：',
					'data' => $this->debug 
			);
			return self::_err ( $err );
		}
		$sn = '';
		$info = array ();
		$info ['sn'] = $sn;
		if($platform=='91YUNGOU'){
			$info ['status'] = '-1';
		}else{
			$info ['status'] = '0';
		}
		$info ['pay_status'] = '0';
		$info ['shipping_status'] = '0';
		$info ['amount'] = '0';
		$info ['count_num'] = '0';
		$info ['add_time'] = time ();
		$info ['pay_time'] = 0;
		$info ['shipping_time'] = 0;
		$info ['add_ip'] = $ip;
		$info ['platform'] = $platform;
		$this->debug = $this->orderinfo = $info;
	} // end _ini_info
	
	/**
	 * 获取订单的扩展信息
	 * @date 2013-5-7
	 * @time 下午2:26:33
	 */
	function order_ext() {
		// 获取订单的扩展信息
		$this->line = 'order_ext::' . __LINE__;
		$this->debug = "orderid:" . $this->orderid;
		if (empty ( $this->orderid )) {
			$err = array (
					"status" => 0,
					'msg' => '订单的主ID不能为空。错误编码：',
					'data' => $this->debug 
			);
			return self::_err ( $err );
		}
		$ext = $this->orderextinfo;
		foreach ( $ext as $v ) {
			$data = array ();
			foreach ( $v as $sk => $sv ) {
					if(in_array($sk,array('element','value','name'))){
						$data [$sk] = $sv;
					}
			}
			if(!empty($data)){
				$data ['order_id'] = $this->orderid;
			}
			$dataall [] = $data;
			$res =$this->orderextdao->add($data);
		}
		//$res = $this->orderextdao->addAll ( $dataall );
		$this->debug = $dataall;
		if ($res === false) {
			$err = array (
					"status" => 0,
					'msg' => '订单的附加信息写入不成功。错误编码：',
					'data' => $this->debug,
					'sql' => $this->orderextdao->getLastSql () 
			);
			return self::_err ( $err );
		}
		return true;
	} // end _order_ext
	
	/**
	 * 写入订单主表信息
	 * 订单商品的总金额及商品数量在订单写入后再更新
	 * @date 2013-5-1
	 * @time 下午4:52:32
	 */
	function w_order() {
		// 写入订单主表信息
		$this->line = 'w_order::' . __LINE__;
		$amount = $this->orderinfo['amount'];
		$this->orderinfo['amount'] = $amount + $this->orderinfo['coupon'];
		$this->orderid = $this->orderdao->add ( $this->orderinfo );
		$this->orderinfo['amount'] = $amount;
		$this->debug = "orderid:" . $this->orderid . '  SQL:' . $this->orderdao->getlastsql ();
		if (empty ( $this->orderid )) {
			$err = array (
					"status" => 0,
					'msg' => '订单的主ID写入不成功。错误编码：',
					'data' => $this->debug 
			);
			return self::_err ( $err );
		}
		self::logs($this->orderinfo, $this->orderid,'创建订单');
		return $this->orderid;
	} // end w_order
	
	/**
	 * 写入订单的产品信息
	 * 检查产品的总数量
	 * @date 2013-5-2
	 * @time 下午6:17:54
	 */
	function w_goods() {
		// 写入订单的产品信息
		$this->line = 'w_goods::' . __LINE__;
		if (empty ( $this->orderid )) {
			$err = array (
					"status" => 0,
					'msg' => '订单的主ID生成错误。错误编码：',
					'data' => $this->debug 
			);
			return self::_err ( $err );
		}
		$goods = $this->goods;
		if (empty ( $goods )) {
			$err = array (
					"status" => 0,
					'msg' => '订单商品信息为空。错误编码：',
					'data' => $this->debug 
			);
			return self::_err ( $err );
		}
		$ordergoods = array ();
		$goodsext = array ();
		foreach ( $goods as $k => $v ) {
			$v ['order_id'] = $this->orderid;
			$temp = array ();
			if ($v ['ext']) {
				$temp = $v ['ext'];
				unset ( $v ['ext'] );
			}
			$goodsid = $this->goodsdao->add ( $v );
			$this->debug = $this->goodsdao->getLastSql ();
			if ($goodsid === false) {
				$err = array (
						"status" => 0,
						'msg' => '订单商品写入不成功。错误编码：',
						'data' => $this->debug 
				);
				return self::_err ( $err );
			}
			if ($temp) {
				$temp ['goods_id'] = $goodsid;
				$temp ['order_id'] = $this->orderid;
				$goodsext [] = $temp;
			}
			$v ['id'] = $goodsid;
			$ordergoods [$k] = $v;
		}
		$this->ordergoodsext = $goodsext;
		return $this->ordergoods = $ordergoods;
	} // end w_goods
	
	/**
	 * 写入订单产品的扩展表
	 * @date 2013-5-6
	 * @time 下午5:10:22
	 */
	function w_goods_ext() {
		// 写入订单产品的扩展表
		$this->line = 'w_goods_ext::' . __LINE__;
		if (! empty ( $this->ordergoodsext )) {
			$this->goodsextdao->addAll ( $this->ordergoodsext );
		}
	} // end w_goods_ext
	
	/**
	 * 写入财务流水
	 * 只有在扣款完成之后才能写入财务流水
	 *
	 * @param Array $finance=array('BV'=>10,'CV'=>'10')
	 *        	@date 2013-5-8
	 *        	@time 下午1:38:58
	 */
	function w_finance($finance = array(),$userid=0) {
		// 写入财务流水
		$this->line = 'w_finance::' . __LINE__;
		if (empty ( $finance )) {
			return false;
		}
		if($this->platform=='SMW01'){
			$userid=$this->userid;
		}
		$this->debug = "userid:" . $userid;
		if (empty ($userid)) {
			$err = array (
					"status" => 0,
					'msg' => '财务信息写入不成功，用户ID不能为空。',
					'data' => $this->debug 
			);
			return self::_err ( $err );
		}
		$this->debug = "orderid:" . $this->orderid;
		if (empty ( $this->orderid )) {
			$err = array (
					"status" => 0,
					'msg' => '财务信息写入不成功，订单ID不能为空。',
					'data' => $this->debug 
			);
			return self::_err ( $err );
		}
		$data = array ();
		$data ['userid'] = $data ['action_userid'] = $userid;
		$data ['order_id'] = $this->orderid;
		$data ['action'] = 'BUY';
		$data ['goods_id'] = '0'; // 只有退款时有关联产品ID
		$data ['number'] = '0';
		$data ['remark'] = '0';
		$data ['cip'] = $this->orderinfo ['add_ip'];
		$data ['code'] = '0';
		$data ['status'] = '0';
		$data ['ctime'] = time ();
		$data ['platform'] = $this->platform;
		$err = array ();
		foreach ( $finance as $k => $v ) {
			$vo = array ();
			$vo = $data;
			$vo ['subject'] =$v['subject'];
			$vo ['value'] = $v['value'];
			$vo ['code'] = $v['code']?$v['code']:'0';
			$vo['remark']=$v['remark']?$v['remark']:$vo['remark'];
			$vo['status']=$v['status'];
			$vo['number']=$v['number'];
			$vo['action_userid']=$this->userid;
			$vo['goods_id']=$v['goods_id'];
			$id = $this->financedao->add ( $vo );
			if (empty ( $id )) {
				$this->debug = $this->financedao->getLastSql ();
				$err [] = $vo;
			}
		}
		if (! empty ( $err )) {
			$err = array (
					"status" => 0,
					'msg' => '财务信息写入出现错误。',
					'data' => $err,
					'debug' => $this->debug 
			);
			return self::_err ( $err );
		}
		return true;
	} // end w_finance
	
	/**
	 * 写入优惠券信息
	 * 优惠券包括代金券、赠品、套餐
	 *
	 * @param array $coupon=array(
	 *        	array('goods_id'=>'3,5','subject'=>'MONEY','value'=>10,'type'=>'套餐','content'=>'序列化的数组'),
	 *        	array('goods_id'=>'0','subject'=>'MONEY','value'=>10,'type'=>'优惠券','content'=>'序列化的数组'),
	 *        	)
	 *        	@date 2013-5-8
	 *        	@time 下午1:40:06
	 */
	function w_coupon($coupon = array()) {
		// 写入优惠券信息
		if (empty ( $coupon )) {
			return false;
		}
		$this->line = 'w_coupon::' . __LINE__;
		$this->debug = "userid:" . $this->userid;
		if (empty ( $this->userid )) {
			$err = array (
					"status" => 0,
					'msg' => '优惠券信息写入不成功，用户ID不能为空。',
					'data' => $this->debug 
			);
			return self::_err ( $err );
		}
		$this->debug = "orderid:" . $this->orderid;
		if (empty ( $this->orderid )) {
			$err = array (
					"status" => 0,
					'msg' => '优惠券信息写入不成功，订单ID不能为空。',
					'data' => $this->debug 
			);
			return self::_err ( $err );
		}
		$this->debug = "userid:" . $this->userid;
		$this->line = 'w_coupon::' . __LINE__;
		if (empty ( $this->userid )) {
			$err = array (
					"status" => 0,
					'msg' => '财务信息写入不成功，用户ID不能为空。',
					'data' => $this->debug 
			);
			return self::_err ( $err );
		}
		$data = array ();
		$data ['userid'] = $this->userid;
		$data ['order_id'] = $this->orderid;
		$err = array ();
		foreach ( $coupon as $v ) {
			$vo = array ();
			$vo = $data;
			$vo ['goods_id'] = $v ['goods_id'];
			$vo ['subject'] = $v ['subject'];
			$vo ['value'] = $v ['value'];
			$vo ['type'] = $v ['type'];
			$id = $this->coupondao->add ( $vo );
			if (empty ( $id )) {
				$this->debug = $this->coupondao->getLastSql ();
				$err [] = $vo;
			}
		}
		if (! empty ( $err )) {
			$err = array (
					"status" => 0,
					'msg' => '财务信息写入出现错误。',
					'data' => $err,
					'debug' => $this->debug 
			);
			return self::_err ( $err );
		}
		return true;
	} // end w_coupon
	
	/**
	 * 校验订单信息
	 * 检查商品数量
	 * 检查商品总金额
	 * @date 2013-5-8
	 * @time 下午1:41:27
	 */
	function chk_order() {
		// 校验订单信息
		$this->debug = "orderid:" . $this->orderid;
		if (empty ( $this->orderid )) {
			$err = array (
					"status" => 0,
					'msg' => '财务信息写入不成功，订单ID不能为空。',
					'data' => $this->debug 
			);
			return self::_err ( $err );
		}
		$order = $this->orderdao->find ( $this->orderid );
		$goods_sum = $this->goodsdao->field ( 'sum(number) numbersum,sum(priceall) pricesum' )->where ( array (
				'order_id' => $this->orderid 
		) )->select ();
		$goods_sum = $goods_sum ['0'];
		$this->debug = $this->goodsdao->getLastSql ();
		if ($goods_sum ['numbersum'] != $order ['count_num']) {
			$this->line = 'chk_order::' . __LINE__;
			$err = array (
					"status" => 0,
					'msg' => '订单信息校验发现错误，产品数量不一致。',
					'data' => $order
			);
			return self::_err ( $err );
		} elseif ($goods_sum ['pricesum'] != $order ['amount'] - $this->orderinfo['coupon']&&$this->platform=='SMW01') {
			$this->line = 'chk_order::' . __LINE__;
			$err = array (
					"status" => 0,
					'msg' => '订单信息校验发现错误，产品金额不一致。',
					'data' => $order,
					'debug' => $this->debug 
			);
			return self::_err ( $err );
		}
		$sn = '';
		if ($this->platform == 'SMW01') {
			$sn = '10' . date ( 'ymdH' ) . $this->orderid;
		} else {
			$sn = '90' . date ( 'ymdH' ). $this->orderid;
		}
		$this->orderdao->where('id='. $this->orderid)->save(array('sn'=>$sn));
		return $sn;
	} // end chk_order
	
	/**
	 * 设置O2O下的订单生效
	 * @param array $oids 订单的ID数组
	 *@date 2013-6-14
	 *@time 下午11:08:01
	 */
	function o2o_status($oids=array()) {
	//设置O2O下的订单生效
		$this->line = 'o2o_status::' . __LINE__;
		$oids=implode(',',$oids);
		$where=array('id'=>array('IN',$oids));
		$res=$this->orderdao->where($where)->save(array('status'=>'0'));
		$this->debug=$this->orderdao->getLastSql();
		if($res!==false){
			return true;
		}else{
			$err = array (
					"status" => 0,
					'msg' => '订单状态设置失败,请重试。',
					'debug' => $this->debug
			);
			return self::_err ( $err );
		}
	}//end o2o_status
	
	/**
	 * 写入订单日志
	 * @param int $userid 用户ID
	 * @param int $orderid 订单ID
	 * @param string $text 日志内容
	 * @param array $opt 其它选项如 open开发用户
	 */
	static function logs($userid,$orderid,$text,$opt=array('open'=>'ALL')){
		$dao=M("OrderLogs");
		$data=array();
		$data['user_id']=$userid;
		$data['order_id']=$orderid;
		$data['content']=$text;
		$data['user_id']=$opt['open'];
		$data['ctime']=time();
		$data['type']=$opt['type']?$opt['type']:ACTION_NAME;
		$dao->add($data);
	}
	 
	 /**
	  * 指定产品特价
	  * @param array  $product 产品
	  * @return array 处理产品
	  */
	 function ext_specia_product($product_id){
		$pro_act = C('P_ACT');//dump($pro_act);die();
		$temp = $pro_act[ $product_id ];
		$data_sub = array();
		if ( $temp ) {
			$p = M('product')->field( 'image,code' )->find($product_id);
			$data_sub ['id']    = $product_id;
			$data_sub ['image'] = $p['image'];
			$data_sub ['name']  = $temp [0];
			$data_sub ['code']  = $p['code'];
			$data_sub ['jiayi'] = $temp [1];
		}
		return $data_sub;
	 	
	 }
	 
	 /**
	  * 返回产品当前时价
	  * @param array $_proids
	  * @return array $info
	  */
	 function realprice($_proids){
	 	$model = M('ProductPriceB2b');
	 	$where = array();
	 	$where['product_id'] = array('IN',implode(',', $_proids));
	 	$field = 'product_id,price,jiayi,jiaer,jiasan,jiasi';
	 	$info = $model->field($field)->where($where)->select();
	 	$info = field2key($info, 'product_id');
	 	return $info;
	 }
	 
	 /**
	  * 根据产品ID返回聚划算优惠信息
	  * @param array $_ids
	  * @param string $_type 优惠类型
	  * @param string $_plat 平台
	  * @return array $Info
	  */
	 function protocoupon($_proids,$_type = "SALE",$_plat = 'SMW01'){
	 	$model = M('ProductCoupon');
	 	$where = array();
	 	$where['product_id'] = array('IN',implode(',', $_proids));
	 	$where['endtime'] = array(array('GT',time()),array('eq',0),'or');
	 	$where['starttime'] = array(array('LT',time()),array('eq',0),'or');
	 	$where['status'] = '1';
	 	$where['platform'] = $_plat;
	 	$where['type'] = $_type;
	 	$info = $model->field('product_id,subject,value,number,frequency')->where($where)->select();
// 	 	echo $model->getLastSql();
	 	$info = field2key($info, 'product_id');
	 	return $info;
	 }
	 
	 /**
	  * 根据产品ID返回大宗采购优惠信息
	  * @param array $_proids
	  * @param string $_type
	  * @return array $Info
	  */
	 function protobig($_proids,$_type = "BIG"){
	 	$model = M('ProductPackage');
	 	$where = array();
	 	$where['product_ids'] = array('IN',implode(',', $_proids));
	 	$where['endtime'] = array(array('GT',time()),array('eq',0),'or');
	 	$where['starttime'] = array(array('LT',time()),array('eq',0),'or');
	 	$where['type'] = $_type;
	 	$info = $model->field('product_ids,special_price,all_special_price,prerequ')->where($where)->select();
// 	 	echo $model->getLastSql();
	 	$info = field2key($info, 'product_ids');
	 	return $info;
	 }
	 
	 /**
	  * 写入优惠信息
	  * @param array $_info
	  * @param string $_orderid
	  * @param string | int $_uid
	  * @return boolean $reid
	  */
	 function wcoupon($_info,$_orderid,$_uid){
	 	$model = M('OrderCoupon');
	 	$data = array();
	 	if($_info['couponprice'] > 0){
	 		$data['order_id'] = $_orderid;
	 		$data['goods_id'] = implode(',',$_info['bigids']);
	 		$data['subject'] = 'CV';
	 		$data['value'] = $_info['couponprice'];
	 		$data['type'] = 'BIG';
	 		$data['userid'] = $_uid;
	 		$data['content'] = "大宗采购优惠";
	 		$reid = $model->add($data);
// 	 		echo $model->getLastSql();
	 		if($reid === FALSE){
	 			return $reid;
	 		}
	 	}

	 	
 		if($_info['bvprice'] > 0){
 			$data['order_id'] = $_orderid;
 			$data['goods_id'] = implode(',',$_info['bvids']);
 			$data['subject'] = 'BV';
 			$data['value'] = $_info['bvprice'];
 			$data['userid'] = $_uid;
 			$data['type'] = 'SALE';
 			$data['content'] = "聚划算优惠";
 			$reid = $model->add($data);
 			// 	 			echo $model->getLastSql();
 			if($reid === FALSE){
 				return $reid;
 			}
 		}
 		
 		return true;
	 }
	 
	 /**
	  * 二维数组返回一维或字符串
	  * @param array $_arr
	  * @param string $_field
	  * @param string $_type 1为数组 2为字符串
	  * @return multitype:array|multitype:string
	  */
	 function _arr2to1($_arr,$_field = 'id',$_type = '1'){
	 	if ( empty( $_arr) ) return array();
	 	$arrayids = array();
	 	foreach ($_arr as $k => $v){
	 		$arrayids[$k] = $v[$_field];
	 	}
	 	if($_type != '1') $arrayids = implode(',',$arrayids);
	 
	 	return $arrayids;
	 }
}
